<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control
      :titles="['流行', '新款', '精选']"
      @tabClick="tabClick"
      ref="tabControl1"
      class="tab-control"
      v-show="isTabFixed"
    />
    <scroll
      class="content"
      ref="scroll"
      :probe-type="3"
      @scrollPosition='contentScroll'
      :is-pull-up-load="true"
      @pullUpLoad="loadMore"
    >
      <home-swiper :banners="banners"
                   @homeSwiperImgLoaded="homeSwiperImgLoaded"
      />
      <home-recommend-view :recommends="recommends"></home-recommend-view>
      <feature-view/>
      <tab-control
        :titles="['流行', '新款', '精选']"
        @tabClick="tabClick"
        ref="tabControl2"
      />
      <goods-list :goods="showGoods"/>
    </scroll>
    <back-top v-show="isShowBackTop" @click.native="backTopClick"/>
  </div>
</template>

<script>
import {getHomeGoods, getHomeMultiData} from "@/network/home";
import HomeSwiper from "@/views/home/children/HomeSwiper";
import HomeRecommendView from "@/views/home/children/HomeRecommendView";
import FeatureView from "@/views/home/children/FeatureView";

import Scroll from "@/components/common/scroll/Scroll";
import NavBar from "@/components/common/navbar/NavBar";

import TabControl from "@/components/content/tabControl/TabControl";
import BackTop from "@/components/content/backTop/BackTop";
import GoodsList from "@/components/content/goods/GoodsList";

import {debounce} from "@/common/utils";

export default {
  name: "Home",
  components: {
    'nav-bar': NavBar,
    'home-swiper': HomeSwiper,
    'home-recommend-view': HomeRecommendView,
    'feature-view': FeatureView,
    'tab-control': TabControl,
    'goods-list': GoodsList,
    'scroll': Scroll,
    'back-top': BackTop,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      keywords: [],
      goods: {
        'pop': {
          page: 0,
          list: []
        },
        'new': {
          page: 0,
          list: []
        },
        'sell': {
          page: 0,
          list: []
        },
      },
      currentType: 'pop',
      isShowBackTop: false,
      tabControlOffsetTop: 0,
      isTabFixed: false,
      positionY: 0,
      saveY: 0
    }
  },
  deactivated() {
    this.saveY = this.$refs.scroll.scroll.y;
  },
  activated() {
    this.$refs.scroll.scrollTo(0, this.saveY, 0);
    this.$refs.scroll.refresh();
  },
  created() {
    this.getHomeMultiData();
    this.getHomeGoods('pop');
    this.getHomeGoods('new');
    this.getHomeGoods('sell');
  },
  computed: {
    showGoods() {
      return this.goods[this.currentType].list;
    }
  },
  mounted() {
    const refresh = debounce(this.$refs.scroll.refresh, 500);
    this.$bus.$on('itemImageLoaded', () => {
      refresh();
    });
  },
  methods: {
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop';
          break;
        case 1:
          this.currentType = 'new';
          break;
        case 2:
          this.currentType = 'sell';
          break;
        default:
          this.currentType = 'pop';
          break;
      }
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    getHomeMultiData() {
      getHomeMultiData()
        .then(res => {
          this.banners = res.data.banner.list;
          this.recommends = res.data.recommend.list;
          this.keywords = res.data.keywords.list;
        })
        .then(err => {
        });
    },
    getHomeGoods(type) {
      getHomeGoods(type, this.goods[type].page + 1).then(res => {
        //如果一直加载应该将数据合并到里面去
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //加载下一页完成后刷新下拉状态，以便下一次下拉
        this.$refs.scroll.finishPullUp();
      });
    },
    backTopClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    /**
     * 置顶按钮是否显示
     * tabContr是否吸顶
     * @param position
     */
    contentScroll(position) {
      this.isShowBackTop = (-position.y) > 1000
      this.isTabFixed = (-position.y) > this.tabControlOffsetTop
    },
    loadMore() {
      this.getHomeGoods(this.currentType);
    },
    homeSwiperImgLoaded() {
      this.tabControlOffsetTop = this.$refs.tabControl2.$el.offsetTop;
    }
  }
};
</script>

<style scoped>
#home {
  /*上内边距44*/
  /*padding-top: 44px;*/
  /*home 高度为视图大小的100%*/
  height: 100vh;

  position: relative;
}

.home-nav {
  background-color: var(--color-tint);
  color: white;
  /*原生滚动使用一下属性*/
  /*position: fixed;*/

  /*top: 0;*/
  /*left: 0;*/
  /*right: 0;*/

  /*z-index: 9;*/
}

.tab-control {
  position: relative;

  z-index: 9;
}

.content {
  overflow: hidden;

  position: absolute;
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}
</style>
